package com.tuanzi.loan.business.repository.receipt.product.impl;

import static com.tuanzi.loan.generated.jooq.loan.tables.LoanReceiptProductInfo.LOAN_RECEIPT_PRODUCT_INFO;

import java.util.List;

import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Repository;

import com.tuanzi.loan.business.entity.receipt.product.ReceiptProductInfo;
import com.tuanzi.loan.business.repository.receipt.product.custom.ReceiptProductInfoRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepository;
import com.tuanzi.loan.vo.request.receipt.product.ReceiptProductInfoSearchRequest;
import com.tuanzi.loan.vo.response.receipt.product.ReceiptProductInfoSearchResponse;
import com.tuanzi.server.loan_base.constant.LoanReceiptProductStatus;

/**
 * @author Ray 2017/09/21
 */
@Repository
@SuppressWarnings({ "rawtypes" })
public class ReceiptProductInfoRepositoryImpl extends JOOQBaseRepository implements ReceiptProductInfoRepositoryCustom {

	@Override
	protected TableImpl getTable() {
		return LOAN_RECEIPT_PRODUCT_INFO;
	}

	@Override
	public Page<ReceiptProductInfoSearchResponse> list(ReceiptProductInfoSearchRequest searchRequest, Pageable pageable) {
		contains(LOAN_RECEIPT_PRODUCT_INFO.NAME, searchRequest.getName());
		contains(LOAN_RECEIPT_PRODUCT_INFO.SUMMARY, searchRequest.getSummary());
		eq(LOAN_RECEIPT_PRODUCT_INFO.ID, searchRequest.getId());
		eq(LOAN_RECEIPT_PRODUCT_INFO.STATUS, searchRequest.getStatus());
		eq(LOAN_RECEIPT_PRODUCT_INFO.JOIN_ONE_KEY, searchRequest.getOneKey());
		in(LOAN_RECEIPT_PRODUCT_INFO.STATUS, searchRequest.getLimitStatus());
		orderBy(LOAN_RECEIPT_PRODUCT_INFO.SORT, Sort.Direction.ASC);
		return search(pageable, new ReceiptProductInfoResponseRecordMapper());
	}

	@Override
	public List<ReceiptProductInfo> findAllProductIdAndName() {
		return dslContext.select(LOAN_RECEIPT_PRODUCT_INFO.ID, LOAN_RECEIPT_PRODUCT_INFO.NAME).from(LOAN_RECEIPT_PRODUCT_INFO)
				.where(LOAN_RECEIPT_PRODUCT_INFO.STATUS.in(LoanReceiptProductStatus.ONLINE.getStatus(), LoanReceiptProductStatus.OFFLINE.getStatus()))
				.fetchInto(ReceiptProductInfo.class);
	}

	private final class ReceiptProductInfoResponseRecordMapper implements RecordMapper<Record, ReceiptProductInfoSearchResponse> {

		@Override
		public ReceiptProductInfoSearchResponse map(Record record) {
			ReceiptProductInfoSearchResponse response = new ReceiptProductInfoSearchResponse();
			response.setId(record.get(LOAN_RECEIPT_PRODUCT_INFO.ID));
			response.setName(record.get(LOAN_RECEIPT_PRODUCT_INFO.NAME));
			response.setSummary(record.get(LOAN_RECEIPT_PRODUCT_INFO.SUMMARY));
			response.setLogo(record.get(LOAN_RECEIPT_PRODUCT_INFO.LOGO));
			response.setId(record.get(LOAN_RECEIPT_PRODUCT_INFO.ID));
			response.setLimitOrderOfDay(record.get(LOAN_RECEIPT_PRODUCT_INFO.LIMIT_ORDER_DAY));
			response.setUnitPrice(record.get(LOAN_RECEIPT_PRODUCT_INFO.UNIT_PRICE));
			response.setStatus(record.get(LOAN_RECEIPT_PRODUCT_INFO.STATUS));
			response.setSort(record.get(LOAN_RECEIPT_PRODUCT_INFO.SORT));
			response.setOneKey(record.get(LOAN_RECEIPT_PRODUCT_INFO.JOIN_ONE_KEY) == null ? false : record.get(LOAN_RECEIPT_PRODUCT_INFO.JOIN_ONE_KEY) == 1);
			return response;
		}
	}

}
